package com.android.accessibility.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.android.accessibility.R;
import com.android.accessibility.base.BaseActivity;
import com.android.accessibility.dialog.FunsDialog;
import com.android.accessibility.provider.DBConstant;
import com.android.accessibility.service.FloatService;
import com.android.accessibility.stat.StatManager;
import com.android.accessibility.utils.Constant;
import com.android.accessibility.utils.Utils;
import com.android.accessibility.view.InputDialog;
import com.android.accessibility.view.InputDialog.OnInputListener;

public class WxHelperActivity extends BaseActivity implements
android.view.View.OnClickListener {

    private TextView mMsgView;
    private Button mSentButton = null;
    private ToggleButton mToggleButton1;
    private ToggleButton mToggleButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accessibility_layout);
        initView();
    }

    private void updateView() {
        String msgContent = PreferenceManager.getDefaultSharedPreferences(this)
                .getString("content", null);
        mMsgView.setText(msgContent);

        boolean isAccessibilityOn = Utils.isAccessibilitySettingsOn(this);
        mToggleButton1.setChecked(isAccessibilityOn);
        if (!isAccessibilityOn) {
            Utils.setAccessibility(this, 0);
        }
        mToggleButton2.setChecked(Utils.isServiceWork(this,
                FloatService.class.getName()));

        int count = StatManager.get(this).getSentCount();
        String text = "清除已发送 : " + count + "个";
        mSentButton.setText(text);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateView();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initView() {
        mToggleButton1 = (ToggleButton) findViewById(R.id.acc_settings);
        mToggleButton1.setOnClickListener(this);
        mToggleButton2 = (ToggleButton) findViewById(R.id.start_stop_floatview);
        mToggleButton2.setOnClickListener(this);

        mSentButton = (Button) findViewById(R.id.sent);
        mSentButton.setOnClickListener(this);

        mMsgView = (TextView) findViewById(R.id.msg_content);
        mMsgView.setOnClickListener(this);

        View view = null;
        view = findViewById(R.id.add_black);
        view.setOnClickListener(this);
        view = findViewById(R.id.check_black);
        view.setOnClickListener(this);
        view = findViewById(R.id.add_white);
        view.setOnClickListener(this);
        view = findViewById(R.id.check_white);
        view.setOnClickListener(this);
        view = findViewById(R.id.start_wx);
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.acc_settings) {
            ToggleButton tb = (ToggleButton) v;
            tb.setChecked(!tb.isChecked());
            Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            startActivitySafely(intent);
        } else if (v.getId() == R.id.start_stop_floatview) {
            boolean isStart = mToggleButton2.isChecked();
            Intent intent = new Intent(this, FloatService.class);
            if (isStart) {
                if (Utils.isAccessibilitySettingsOn(this)) {
                    intent.putExtra("class_name", this.getClass().getName());
                    startService(intent);
                } else {
                    mToggleButton2.setChecked(false);
                    Toast.makeText(this, "辅助功能未开启", Toast.LENGTH_SHORT).show();
                }
            } else {
                stopService(intent);
            }
        } else if (v.getId() == R.id.add_black) {
            addBlack();
        } else if (v.getId() == R.id.check_black) {
            checkBlack();
        } else if (v.getId() == R.id.add_white) {
            addWhite();
        } else if (v.getId() == R.id.check_white) {
            checkWhite();
        } else if (v.getId() == R.id.msg_content) {
            setMsgContent();
        } else if (v.getId() == R.id.start_wx) {
            selectFunction();
        } else if (v.getId() == R.id.sent) {
            StatManager.get(this).clearSent();
            updateView();
        }
    }

    private void setMsgContent() {
        InputDialog dialog = new InputDialog(this);
        dialog.setTitle("请输入消息内容");
        dialog.setOnInputListener(new OnInputListener() {
            @Override
            public void onInput(String text) {
                if (!TextUtils.isEmpty(text)) {
                    PreferenceManager
                    .getDefaultSharedPreferences(
                            getApplicationContext()).edit()
                            .putString("content", text).commit();
                    Toast.makeText(getApplicationContext(), "设置成功",
                            Toast.LENGTH_SHORT).show();
                    updateView();
                }
            }
        });
        dialog.show();
    }

    private void addBlack() {
        InputDialog dialog = new InputDialog(this);
        dialog.setTitle("请输入要加入黑名单的昵称");
        dialog.setOnInputListener(new OnInputListener() {
            @Override
            public void onInput(String text) {
                if (!TextUtils.isEmpty(text)) {
                    StatManager.get(getApplicationContext()).insertData(text,
                            System.currentTimeMillis(), DBConstant.TYPE_LIST,
                            DBConstant.FUNS_BLACK);
                    Toast.makeText(getApplicationContext(), "添加成功",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.show();
    }

    private void checkBlack() {
        FunsDialog dialog = new FunsDialog(this, DBConstant.FUNS_BLACK);
        dialog.show();
    }

    private void addWhite() {
        InputDialog dialog = new InputDialog(this);
        dialog.setTitle("请输入要加入白名单的昵称");
        dialog.setOnInputListener(new OnInputListener() {
            @Override
            public void onInput(String text) {
                if (!TextUtils.isEmpty(text)) {
                    StatManager.get(getApplicationContext()).insertData(text,
                            System.currentTimeMillis(), DBConstant.TYPE_LIST,
                            DBConstant.FUNS_WHITE);
                    Toast.makeText(getApplicationContext(), "添加成功",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.show();
    }

    private void checkWhite() {
        FunsDialog dialog = new FunsDialog(this, DBConstant.FUNS_WHITE);
        dialog.show();
    }

    private void selectFunction() {
        if (!Utils.isAccessibilitySettingsOn(this)) {
            Toast.makeText(this, "辅助功能未开启", Toast.LENGTH_SHORT).show();
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择功能");
        final String items[] = new String[] { "发送消息", "抢红包" };
        builder.setItems(items, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (items[which].equals("发送消息")) {
                    Utils.setAccessibility(getBaseContext(),
                            Constant.ACCESSIBILITY_SENDMSG);
                    Toast.makeText(getBaseContext(), items[which],
                            Toast.LENGTH_SHORT).show();
                } else if (items[which].equals("抢红包")) {
                    Utils.setAccessibility(getBaseContext(),
                            Constant.ACCESSIBILITY_WXGRAB);
                    Toast.makeText(getBaseContext(), items[which],
                            Toast.LENGTH_SHORT).show();
                }
                startWx();
            }
        });
        builder.create().show();
    }

    private void startWx() {
        Intent intent = new Intent(Constant.ACTION_RESET);
        startService(intent);
        intent = getPackageManager().getLaunchIntentForPackage(
                Constant.WX_PACKAGENAME);
        startActivitySafely(intent);
    }

    private void startActivitySafely(Intent intent) {
        try {
            startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(this, "未找到相应的功能模块", Toast.LENGTH_SHORT).show();
        }
    }
}
